package com.ly.elasticserach.service;

import com.ass.elasticsearch.service.EasyEsBaseService;
import com.ly.elasticserach.empty.EasyEsTest;
import com.ly.elasticserach.mapper.EasyEsTestMapper;
import org.dromara.easyes.annotation.rely.Analyzer;
import org.dromara.easyes.annotation.rely.FieldType;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.biz.SAPageInfo;
import org.dromara.easyes.core.conditions.index.LambdaEsIndexWrapper;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.dromara.easyes.core.core.EsWrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EasyESTestService extends EasyEsBaseService<EasyEsTest, EasyEsTestMapper> {

    @Autowired
    private EasyEsTestMapper easyEsTestMapper;




    /**
     * @describe：复杂场景使用，更加灵活
     * @author：李勇
     * @date：2025/11/6
     * @param indexName
     * @return：boolean
     */
    public boolean createIndex(String indexName){
        // 复杂场景使用
        LambdaEsIndexWrapper<EasyEsTest> wrapper = new LambdaEsIndexWrapper<>();
        //设置索引名称
        wrapper.indexName(indexName);
        //设置索引结构
        // 此处将文章标题映射为keyword类型(不支持分词),文档内容映射为text类型(支持分词查询)
        wrapper.mapping(EasyEsTest::getBookId, FieldType.KEYWORD)
                .mapping(EasyEsTest::getTitle, FieldType.TEXT,Analyzer.IK_SMART, Analyzer.IK_MAX_WORD)
                .mapping(EasyEsTest::getAuthor, FieldType.KEYWORD)
                .mapping(EasyEsTest::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD)
                .mapping(EasyEsTest::getCreateDate, FieldType.DATE,"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis");

        // 0.9.8+版本,增加对符串字段名称的支持,Document实体中须在对应字段上加上@Tablefield(value="wu-la")用于映射此字段值
        //二选一
//        wrapper.mapping("wu-la", FieldType.TEXT, Analyzer.IK_MAX_WORD, Analyzer.IK_MAX_WORD);

        // 设置分片及副本信息,可缺省
        wrapper.settings(6, 6);

        return easyEsTestMapper.createIndex(wrapper);
    }






    public List<EasyEsTest> list(EasyEsTest model){
        LambdaEsQueryWrapper<EasyEsTest> wrapper = new LambdaEsQueryWrapper<>();
        //使用于fieldType = FieldType.TEXT类型的搜索
        wrapper.match(model.getTitle()!=null,EasyEsTest::getTitle,model.getTitle());
        //fieldType = FieldType.KEYWORD类型的搜索
        wrapper.eq(model.getAuthor()!=null,EasyEsTest::getAuthor,model.getAuthor());
        return easyEsTestMapper.selectList(wrapper);
    }


    public EsPageInfo<EasyEsTest> page(EsPageInfo page, EasyEsTest model){
        LambdaEsQueryWrapper<EasyEsTest> wrapper = EsWrappers.lambdaQuery(EasyEsTest.class);
        //基于条件构造 自己根据需求构造和mybatis-plus 语法一样
        return pageWrapper(page,wrapper);
    }


    public SAPageInfo<EasyEsTest> searchAfter(SAPageInfo<EasyEsTest> page,EasyEsTest model){
        LambdaEsQueryWrapper<EasyEsTest> wrapper = EsWrappers.lambdaQuery(EasyEsTest.class)
                .eq(model.getAuthor()!=null,EasyEsTest::getAuthor,model.getAuthor())
                .match(model.getTitle()!=null, EasyEsTest::getTitle,model.getTitle()); // TEXT字段使用match
        //必须指定排序 不建议使用时间，因为有可能时间相同导致查询的数据混乱
        //基于条件构造 自己根据需求构造和mybatis-plus 语法一样
        wrapper.orderByDesc(EasyEsTest::getBookId);
        return pageSearchAfter(wrapper,page);
    }



}
